﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XNALibrary;
using Microsoft.Xna.Framework.Graphics;
using FarseerGames.FarseerPhysics.Dynamics;
using Microsoft.Xna.Framework;

namespace NetPong.PongClasses3d
{
    public delegate Body BodyGetter();

    public class Body3d : RigidBody
    {
        public float elevation;
        public float pitch;
        public BodyGetter GetBody;
        
        
        public Body3d(Model m, BodyGetter bg)
            : base(m)
        {
            GetBody = bg;
            elevation = 0f;
            pitch = 0f;
        }

        /// <summary>
        /// Position is read-only for Body3d since is aggregates body in the PhysicsSimulator
        /// </summary>
        public override Vector3 Position
        {
            get
            {
                return new Vector3(GetBody().Position.X, elevation, GetBody().Position.Y);
            }
            set
            { GetBody().Position = new Vector2(value.X, value.Y); }
        }

        public override Vector3 LinearVelocity
        {
            get
            {
                return new Vector3(GetBody().LinearVelocity.X, GetBody().LinearVelocity.Y, 0f);
            }
            set
            {
                GetBody().LinearVelocity.X = LinearVelocity.X;
                GetBody().LinearVelocity.Y = linearVelocity.Y;
            }
        }

        public override Vector3 AngularVelocity
        {
            get
            {
                return new Vector3((float)Math.Cos(GetBody().AngularVelocity), (float)Math.Sin(GetBody().AngularVelocity), 0f);
            }
            set
            {
                //not sure about this
                GetBody().AngularVelocity = (float)Math.Acos((double)value.X);
            }
        }

        public override Matrix Rotation
        {
            get
            {
                return Matrix.CreateFromYawPitchRoll(GetBody().Rotation, pitch, 0f);
            }
            set
            {
                //not sure about this either
                GetBody().Rotation = (float)Math.Acos((double)value.Forward.X);
            }
        }
    }
}
